Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
Using a procedure to update Column 2 in a Table A, by checking if column 1 value is present in a column in Table B [PostgreSQL]

I have two tables as follows:

Table A: Name, ID, value

Table B: ID, title

TableA.value is dependent on whether TableA.ID is present in TableB.ID.

I am trying to create a procedure and trigger so that whenever TableB is modified, the procedure is triggered to check if TableA.ID is in TableB.ID and sets TableA.value to 10.

I am using the following code and getting an error:

CREATE PROCEDURE update1
LANGUAGE SQL
AS $$
UPDATE tablA as a
SET a.value = 10
WHERE a.ID EXISTS ( SELECT b.ID 
                            FROM tableB as b
                           WHERE a.ID = b.ID)
$$;

I am an SQL noob, and this is the first time I am trying to use a procedure.

UPDATE

I was able to create a procedure which does the job when I manually run it using CALL

However, it does not have a RETURNS TRIGGER block within it. Adding that returns the error Procedure cannot return triggers.

If I create a trigger as follows

CREATE TRIGGER b_trigger
AFTER UPDATE OR INSERT ON b.ID
FOR EACH STATEMENT
EXECUTE PROCEDURE update1();

However, this returns the following

ERROR: function columbia_deli.manager_discount must return type trigger

SQL state: 42P17

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Try this using plpgsql Language:

Trigger Function:

CREATE OR REPLACE FUNCTION update1()
  RETURNS trigger AS
$BODY$
 
begin
update "TableA" set value= 10 where id =new.id;

return NEW;
end;

$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

Then create trigger on TableB for after update event

CREATE TRIGGER trig_tableb
  AFTER UPDATE
  ON "TableB"
  FOR EACH ROW
  EXECUTE PROCEDURE update1();
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda